package my.graduation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import my.graduation.dto.ClinicInfoDto;
import my.graduation.dto.DepartInfoDto;
import my.graduation.dto.DocRegScheDto;
import my.graduation.entity.Clinic;
import my.graduation.entity.Department;
import my.graduation.entity.Doctor;
import my.graduation.enums.HttpCodeEnums;
import my.graduation.mapper.ClinicMapper;
import my.graduation.mapper.DepartmentMapper;
import my.graduation.mapper.DoctorMapper;
import my.graduation.result.Result;
import my.graduation.service.ClinicService;
import my.graduation.service.DepartmentService;
import my.graduation.vo.ClinicVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Author: RavenLyn
 * @Date: 2024-12-09
 */
@Service
public class ClinicServiceImpl extends ServiceImpl<ClinicMapper, Clinic> implements ClinicService {


    @Autowired
    private DepartmentMapper departmentMapper;

    @Autowired
    private ClinicMapper clinicMapper;

    /**
     * 获取门诊信息
     * @return
     */
    @Override
    public Result<List<ClinicVO>> getList() {
        // 1 获取clinicList
        List<Clinic> clinicList  = list();

        return getListMethod(clinicList);
    }

    /**
     * 条件获取门诊信息
     * @return
     */
    @Override
    public Result<List<ClinicVO>> getListByCon(ClinicInfoDto dto) {
        // 1 获取clinicList
        List<Clinic> clinicList = list(Wrappers.<Clinic>lambdaQuery()
                .eq(dto.getDepartmentId() != null, Clinic::getDepartmentId, dto.getDepartmentId())
                        .like(dto.getName() != null,Clinic::getName,dto.getName()));

        return getListMethod(clinicList);
    }

    /**
     * 获取门诊信息封装method
     * @param clinicList
     * @return
     */
    public Result<List<ClinicVO>> getListMethod(List<Clinic> clinicList){
        // 2 从clinicList中提取所有的departmentId
        List<Integer> departIdList = clinicList.stream()
                .map(Clinic::getDepartmentId)
                .collect(Collectors.toList());

        // 3 查询出departmentList
        List<Department> departmentList = departmentMapper.selectBatchIds(departIdList);

        // 4 封装成map，提高查询性能
        Map<Integer, String> departNameMap = departmentList.stream()
                .collect(Collectors.toMap(Department::getId, Department::getName));

        // 5 装到clinicVO中
        List<ClinicVO> clinicVOList = new ArrayList<>();
        for (Clinic clinic : clinicList){
            // 使用map集合，将O(n)降低成O(1)
            String departmentName = departNameMap.getOrDefault(clinic.getDepartmentId(),null);
            clinicVOList.add(new ClinicVO(clinic, departmentName));
        }

        return Result.success(clinicVOList);
    }

    /**
     * 新增门诊信息
     * @return
     */
    @Override
    public Result add(ClinicInfoDto dto) {
        //1 非空
        if (StringUtils.isNotBlank(dto.getName()) &&
                dto.getDepartmentId() != null ){
            Clinic clinic = new Clinic();
            BeanUtils.copyProperties(dto, clinic);
            boolean i = save(clinic);
            if (i) {
                return Result.success();
            } else {
                return Result.error(HttpCodeEnums.SYSTEM_ERROR);
            }
        }else{
            return Result.error(HttpCodeEnums.INFO_IS_BLANK);
        }
    }


    /**
     * 修改门诊信息
     * @return
     */
    @Override
    public Result update(ClinicInfoDto dto) {
        //1 非空
        if (dto.getId() != null) {
            Clinic clinic = new Clinic();
            BeanUtils.copyProperties(dto, clinic);
            boolean i = updateById(clinic);
            if (i) {
                return Result.success();
            } else {
                return Result.error(HttpCodeEnums.SYSTEM_ERROR);
            }
        }else{
            return Result.error(HttpCodeEnums.DATA_NOT_EXIST);

        }
    }

    @Autowired
    private DoctorMapper doctorMapper;

    /**
     * 删除门诊信息
     * @return
     */
    @Override
    public Result delete(ClinicInfoDto dto) {
        //1 非空
        if (dto.getId()!=null) {
            List<Doctor> doctorList = doctorMapper.selectList(Wrappers.<Doctor>lambdaQuery().eq(Doctor::getClinicId, dto.getId()));
            if(!doctorList.isEmpty()){
                return Result.error(HttpCodeEnums.ILLEGAL_OPERATION);
            }
            Clinic clinic = new Clinic();
            BeanUtils.copyProperties(dto, clinic);
            boolean i = removeById(clinic);
            if (i) {
                return Result.success();
            } else {
                return Result.error(HttpCodeEnums.SYSTEM_ERROR);
            }
        }else{
            return Result.error(HttpCodeEnums.DATA_NOT_EXIST);
        }
    }

    /**
     * 医生根据clinicId获取clinicName
     * @param dto
     * @return
     */
    @Override
    public Result<String> getNameById(DocRegScheDto dto) {
        if(dto.getClinicId() != null){
            int id = dto.getClinicId();
            String name = clinicMapper.getNameById(id);
            return Result.success(name);
        }else {
            return Result.error(HttpCodeEnums.SYSTEM_ERROR);
        }
    }
}
